<?php
/**
 * Created by PhpStorm.
 * Author: sitenv@aliyun.com
 * CreateTime: 2023/8/17 06:42
 * Blog：www.myblogs.xyz
 */

namespace app\interfaces\service;

use app\domain\entity\OrderEntity;
use app\domain\repository\OrderRepositoryImpl;
use app\infrastructure\base\BaseService;

class OrderService extends BaseService
{
    /**
     * 获取订单信息
     * @param array $param
     * @return array
     */
    public static function orderInfo(array $param): array
    {
        $limit = empty($param['page']) ? '' : self::getApiPage($param['page']);
        if ($param['action'] == 'getList'){
            return OrderRepositoryImpl::getList([], $limit);
        }
        if ($param['action'] == 'getShow' && !empty($param['order_id'])){
            return OrderRepositoryImpl::getShow($param['order_id']);
        }
        return [];
    }

    /**
     * 创建订单
     * @param int   $userId
     * @param float $amount
     * @return bool
     */
    public static function createOrder(int $userId, float $amount): bool
    {
        $order = new OrderEntity($userId, $amount);
        $orderRepository = new OrderRepositoryImpl();
        $orderRepository->save($order);
        return true;
    }

    /**
     * 查看订单详情
     * @param int $id
     * @return array
     */
    public static function getOrderById(int $id): array
    {
        $orderRepository = new OrderRepositoryImpl();
        return $orderRepository->getById($id);
    }
}